﻿using System;
using System.Collections.Generic;
using NpcFramework;
using NpcFramework.Traffic;
using UnityEngine;

namespace Yoozoo.Gameplay.Liberty.AI
{
    public class TrafficNpcTrigger : MonoBehaviour
    {
        public static Action<TrafficNpcTrigger> onEnableNpcTrigger;

        private bool _greenState;
        public bool greenState
        {
            get => _greenState;
            set
            {
                _greenState = value;
                OnChangeState();
            }
        }
        private bool _yellowState;

        public bool yellowState
        {
            get => _yellowState;
            set
            {
                _yellowState = value;
                OnChangeState();
            }
        }
        
        //public bool redState => !greenState && !yellowState;

        public TrafficLightState lightState = TrafficLightState.Red;

        public NpcWayPoint pathPoint;
        public List<TrafficNpcWaitPoint> waitPoints;


        private void Start()
        {
            onEnableNpcTrigger?.Invoke(this);
        }

        private void OnChangeState()
        {
            if (greenState)
            {
                lightState = TrafficLightState.Green;
            }
            else if (yellowState)
            {
                lightState = TrafficLightState.Yellow;
            }
            else
            {
                lightState = TrafficLightState.Red;
            }
            
        }
        
#if UNITY_EDITOR

        private Vector3 GIZMOS_OFFSET = new Vector3(0, 0.05f, 0);
        private void OnDrawGizmos()
        {
            if (pathPoint && waitPoints!=null)
            {
                Gizmos.color = Color.green;
                foreach (var waitPoint in waitPoints)
                {
                    if (waitPoint!=null)
                    {
                        Gizmos.DrawLine(pathPoint.transform.position + GIZMOS_OFFSET,waitPoint.transform.position + GIZMOS_OFFSET);
                    }
                }
            }
        }        
#endif
        
    }
}